local yanyu = fk.CreateSkill {

  name = "joy_mou__yanyu",

  tags = {  },

}



yanyu:addEffect(fk.EventPhaseStart, {
  name = "joy_mou__yanyu",
  anim_type = "support",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(yanyu.name) and target.phase == Player.Play and not player:isNude()
  end,
  on_cost = function(self, event, target, player, data)
    local card = player.room:askForDiscard(player, 1, 1, true, yanyu.name, true, ".", "#joy_mou__yanyu-cost", true)
    if #card > 0 then
      event:setCostData(self, card)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card_type = Fk:getCardById(event:getCostData(self)[1]):getTypeString()
    room:throwCard(event:getCostData(self), yanyu.name, player, player)
    local x = 2 - player:usedSkillTimes("#joy_mou__yanyu_delay", Player.HistoryTurn)
    if not player.dead and x > 0 then
      room:setPlayerMark(player, "@joy_mou__yanyu-phase", {card_type, x})
    end
  end,
})

yanyu:addEffect(fk.AfterCardsMove, {
  name = "#joy_mou__yanyu_delay",
  anim_type = "support",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player.dead or player:usedSkillTimes(yanyu.name, Player.HistoryTurn) > 1 then return false end
    local mark = player:getMark("@joy_mou__yanyu-phase")
    if type(mark) == "table" and #mark == 2 then
      local type_name = mark[1]
      local ids = {}
      local id = -1
      local room = player.room
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            id = info.cardId
            if Fk:getCardById(id):getTypeString() == type_name and room:getCardArea(id) == Card.DiscardPile then
              table.insertIfNeed(ids, id)
            end
          end
        end
      end
      ids = room.logic:moveCardsHoldingAreaCheck(ids)
      if #ids > 0 then
        event:setCostData(self, ids)
        return true
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    local ids = table.simpleClone(event:getCostData(self))
    while true do
      self.cancel_cost = false
      self:doCost(event, nil, player, ids)
      if self.cancel_cost then
        self.cancel_cost = false
        break
      end
      if player.dead or player:usedSkillTimes(yanyu.name, Player.HistoryTurn) > 1 then break end
      ids = U.moveCardsHoldingAreaCheck(room, ids)
      if #ids == 0 then break end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "joy_mou__yanyu_cards", data)
    local _, ret = room:askForUseActiveSkill(player, "joy_mou__yanyu_active", "#yanyu-choose", true, nil, true)
    room:setPlayerMark(player, "joy_mou__yanyu_cards", 0)
    if ret then
      event:setCostData(self, {ret.targets[1], ret.cards[1]})
      return true
    end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    player:broadcastSkillInvoke("joy_mou__yanyu")
    local mark = player:getMark("@joy_mou__yanyu-phase")
    if type(mark) == "table" or #mark == 2 then
      local x = 2 - player:usedSkillTimes(yanyu.name, Player.HistoryTurn)
      player.room:setPlayerMark(player, "@joy_mou__yanyu-phase", x > 0 and {mark[1], x} or 0)
    end
    player.room:obtainCard(event:getCostData(self)[1], event:getCostData(self)[2], true, fk.ReasonGive)
  end,
})

return yanyu